package util

import (
	"github.com/satori/go.uuid"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func DownloadImg(dir, url string) {
	uid := uuid.NewV4()
	fileName := uid.String() + ".jpg"
	filePath := dir + fileName
	newFile, err := os.Create(filePath)

	if err != nil {
		log.Fatal(err)
	}
	defer newFile.Close()

	client := http.Client{Timeout: 9000 * time.Second}
	resp, err := client.Get(url)

	defer resp.Body.Close()

	_, err = io.Copy(newFile, resp.Body)
	if err != nil {
		log.Fatal(err)
	}
}
